package user_information;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import DB.DB_connection_pool;

public class Phone_modify extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		JSONObject jsonobj = new JSONObject();
		
		String user_account = request.getSession().getAttribute("user_account").toString();
		String session_verification_code = request.getSession().getAttribute("verification_code").toString();   //获取存在session的验证码
		
		String new_phone = request.getParameter("new_phone");
		String verification_code = request.getParameter("verification_code");
		
		String update_sql = "update land_table set user_telephone = '"+new_phone+"' where user_account = '"+user_account+"'";
		
		if(session_verification_code.equals(verification_code)){
			try {
				Connection conn = DB_connection_pool.data_pool.getConnection();
				PreparedStatement ps_update = conn.prepareStatement(update_sql);
				ps_update.executeUpdate();        //更新绑定手机
				ps_update.close();
				
				String update_checking = "update user_data set is_telephone_checking = '1' where user_account = '"+user_account+"'";
				PreparedStatement checking_update = conn.prepareStatement(update_checking);
				checking_update.executeUpdate();        //更新绑定手机
				checking_update.close();
				
				conn.close();
				jsonobj.put("result", true);
			} catch (SQLException e) {
				jsonobj.put("result", false);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			jsonobj.put("result", false);
		}
		out.print(jsonobj.toString());
	}

}
